<template>
    <div>
        <h3 id="title">修改管理员信息</h3>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="账号名" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="角色">
                <span v-for="role in roles">
                    {{role.name}}
                </span>
            </el-form-item>
<!--            <el-form-item label="权限">-->
<!--                <span v-for="role in ruleForm.manageRoles">-->
<!--                    <span v-for="perm in role.managePermissions">-->
<!--                        {{perm.name}}-->
<!--                    </span>-->
<!--                </span>-->
<!--            </el-form-item>-->
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "updateOneManager",
        data(){
            var validateName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入名字'));
                } else {
                    callback();
                }
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value == '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return{
                ruleForm: {
                    name:'',
                    pass:'',
                    checkPass:''
                },
                id:'',
                rules: {
                    name:[
                        {validator:validateName,trigger:'blur'}
                    ],
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ]
                },
                roles:{}
            }
        },
        methods:{
            getManagerById(){
                let id = this.$route.query.id;
                this.$axios.get("manage/getOneManagerInfo",{params:{id:id}}).then(response=>{
                    this.ruleForm.name = response.data.manager.name;
                    this.ruleForm.pass = response.data.manager.pass;
                    this.ruleForm.id =response.data.manager.id;
                    this.roles=response.data.manager.manageRoles;
                });
            },
            resetForm(formName){
                this.$refs[formName].resetFields();
            },
            submitForm(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post("manage/updateOneManager",this.ruleForm).then(response=>{
                            if (response.data.code==200) {
                                this.$router.push("/manage/managerList");
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            }
        },
        created() {
            this.getManagerById();
        },
        watch:{
            $route:function () {
                this.getManagerById();
            }
        }
    }
</script>

<style scoped>
    #title{
        margin-left: 400px;
    }
</style>